package com.siki.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.siki.dto.admin.FindEnterpriseListDTO;
import com.siki.dto.admin.SumbitAuditStatusDTO;
import com.siki.mapper.EnterpriseMapper;
import com.siki.mapper.GeneralUserMapper;
import com.siki.mapper.UserMapper;
import com.siki.pojo.commonEntity.GeneralUser;
import com.siki.pojo.commonEntity.SysUser;
import com.siki.pojo.commonEntity.TalentPosition;
import com.siki.pojo.enumeration.common.AuditStatus;
import com.siki.pojo.enumeration.common.UserRole;
import com.siki.service.AdminService;
import com.siki.vo.admin.FindAllEnterpriseVO;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
/**
 * @Author Siki
 * @Date 2020/6/9
 */

@Service
public class AdminServiceImpl implements AdminService {

    private final UserMapper userMapper;
    private final EnterpriseMapper enterpriseMapper;
    private final GeneralUserMapper generalUserMapper;

    public AdminServiceImpl(UserMapper userMapper, EnterpriseMapper enterpriseMapper, GeneralUserMapper generalUserMapper) {
        this.userMapper = userMapper;
        this.enterpriseMapper = enterpriseMapper;
        this.generalUserMapper = generalUserMapper;
    }


    @Override
    public IPage<FindAllEnterpriseVO> findAllEnterprise(FindEnterpriseListDTO dto) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        Optional.ofNullable(dto.getUserRole()).ifPresent(role -> {
            queryWrapper.eq("user_role",role);
        });
        Optional.ofNullable(dto.getRealName()).ifPresent(realName->{
            queryWrapper.eq("real_name",realName);
        });
        queryWrapper.isNotNull("audit_status").eq("audit_status",dto.getAuditStatus());
        Page<SysUser> page = new Page<>(dto.getPageNum() == null ? 1 : dto.getPageNum(), dto.getPageSize() == null ? 10 : dto.getPageSize());
        return userMapper.selectPage(page,queryWrapper).convert(FindAllEnterpriseVO::new);
    }

    @Override
    public SysUser findAccount(String id) {
        return getUserById(id);
    }

    @Override
    public void sumbitAccountStatus(SumbitAuditStatusDTO dto, String id) {
        SysUser sysUser = getUserById(dto.getId());
        if(sysUser.getUserRole().equals(UserRole.BUSINESS_USER)) {
            //企业用户
            sysUser.setAuditStatus(Optional.ofNullable(dto.getAuditStatus()).map(AuditStatus::valueOf).orElse(null));
        }else if(sysUser.getUserRole().equals(UserRole.GENERAL_USER)){
            //普通用户
            GeneralUser generalUser = sysUser.getGeneralUser();
            generalUser.setForbid(Optional.ofNullable(dto.getForbid()).orElse(null));
            generalUserMapper.updateById(generalUser);
        }
        sysUser.setAuditUserName(getUserById(id).getRealName());
        userMapper.updateById(sysUser);
    }

    @Override
    public List<TalentPosition> getComplaintList() {

        return null;
    }


    private SysUser getUserById(String id){
        return Optional.ofNullable(userMapper.selectById(id))
                .orElseThrow(()->new RuntimeException("该id:"+id+"指定用户不存在"));
    }
}
